const jwt = require('jsonwebtoken');
module.exports = () => {
    return async (ctx,next) => {
        //1.白名单
        let writePath = ['/api/login','/api/list'];

        if(writePath.includes(ctx.path)){
            //不需要检验
            await next();
        }else{
            //需要检验
            let token = ctx.get('token');

            console.log(token)

            try{
                jwt.verify(token,'lixd');
                await next();
            }catch(e){
                if(e.name === 'TokenExpiredError' || e.name === 'JsonWebTokenError'){
                    ctx.status = 401;
                    ctx.body = {
                        code:4,
                        msg:e
                    }
                }else{
                    console.log("======================================")
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }
        }
    }
}